home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dnsconf / timestr.c < prev   
C/C++ Source or Header  |  1995-09-24  |  2KB  |  83 lines

  1. #include <stdlib.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include "../misc/misc.h"
  5. #include "internal.h"
  6.  
  7.  
  8. /* #Specification: dnsconf / times / display format
  9.     A DNS manage time in seconds. dnsconf display those times
  10.     using the following format, which makes it easier for user:
  11.  
  12.     #
  13.         d:hh:mm:ss
  14.  
  15.         d  = number of days
  16.         hh = number of hours
  17.         mm = number of minutes
  18.         ss = number of seconds
  19.     #
  20.  
  21.     dnsconf also accept user input with the same format except
  22.     that the leftmost components are optionnal. This means that
  23.     the user may enter a huge number (a lot of seconds), or break
  24.     this number is days, hours, ... The input format is
  25.     then
  26.  
  27.     #
  28.         [[[days:]hours:]minutes:]seconds
  29.     #
  30.     Where the items inside the square brackets are optionnals.
  31.  
  32. */
  33. #
  34.  
  35. PRIVATE void TIMESTR::formatstr()
  36. {
  37.     long days = seconds / (24*60*60);
  38.     long remain = seconds % (24*60*60);
  39.     long hours = remain / (60*60);
  40.     remain = seconds % (60*60);
  41.     int minutes = remain / 60;
  42.     int secs = remain % 60;
  43.     char buf[20];
  44.     sprintf (buf,"%ld:%02ld:%02d:%02d",days,hours,minutes,secs);
  45.     SSTRING::setfrom (buf);
  46. }
  47.  
  48. /*
  49.     Accepte a string of the form [[[days:]hours:]minutes:]seconds
  50. */
  51. PUBLIC void TIMESTR::setfrom(const char *_str)
  52. {
  53.     long res[8];
  54.     memset (res,0,sizeof(res));
  55.     int nb=4;
  56.     while (isdigit(*_str) && nb < 8){
  57.         res[nb++] = atol(_str);
  58.         while (isdigit(*_str)) _str++;
  59.         if (*_str != ':') break;
  60.         _str++;
  61.     }
  62.     setfrom (res[nb-4]*24*60*60
  63.         +res[nb-3]*60*60
  64.         +res[nb-2]*60
  65.         +res[nb-1]);
  66. }
  67. PUBLIC void TIMESTR::setfrom(long _seconds)
  68. {
  69.     seconds = _seconds;
  70.     formatstr();
  71. }
  72.  
  73. PUBLIC TIMESTR::TIMESTR (long _seconds)
  74. {
  75.     setfrom (_seconds);
  76. }
  77.  
  78. PUBLIC TIMESTR::TIMESTR()
  79. {
  80.     setfrom ((long)0);
  81. }
  82.  
  83.